#$dir ="/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_emerge/clean/beagle/run_beagle/programtest";
#$file = "$dir/whole_gprobs";
#$file_tped = "$dir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInital_check_ambi1.tped";
#$femalefile = "$dir/23/female/beagle_out_female.geno_female.bgl.gz.gprobs";
#$malefile = "$dir/23/male/beagle_out_male.geno_male.bgl.gz.phased";
#!/usr/bin/perl
use Getopt::Std;
getopt("d:g:t:f:m", \%args);
my $dir = $args{d};
my $file = $args{g};
my $file_tped= $args{t};
my $femalefile= $args{f};
my $malefile = $args{m};
chomp $dir;
chomp $file;
chomp $file_tped;
chomp $femalefile;
chomp $malefile;
if($dir eq "")
{
	die "enter directory is empty\n";
}
if($file eq "")
{
        die "enter whole gprobs filename is empty\n";
}
if($file_tped eq "")
{
        die "entered combined tped filename is empty\n";
}
if($femalefile eq "")
{
        die "entered female geno filename is empty\n";
}
if($malefile eq "")
{
        die "entered male geno filename is empty\n";
}


#$dir ="/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_emerge/clean/beagle/run_beagle/beagle_mainrun/test";
#$file = "$dir/test.gprobs";
#$file_tped = "$dir/geno.tped";
#$femalefile = "$dir/female.gprobs";
#$malefile = "$dir/male.geno";


open TBUFF,"< $file_tped" or die "Cant open $file_tped";
open BUFF, "< $file" or die "Cant open $file";
$line = <BUFF>;
@array_ids = split(/\s/,$line);
$line = "SNP A1 A2";
$num = 0;
$numm_count = 0;
for($z=3;$z<@array_ids;$z++)
{
	$array_sam[$num] = $array_ids[$z];
	$num++;
	$line = $line." $array_ids[$z]";
	$z++; 
#	$line = $line." $array_ids[$z]";
	$z++;
}
open(WRBUFF,">$dir/whole_chr_gprobs_dosage3");
#print WRBUFF $line."\n";

while($line = <BUFF>)
{
			
	chomp($line);
	@array = split(/\s/,$line);
	$line = $array[0]." ".$array[1]." ".$array[2];
	$hash_main{$array[0]} = 10;
	for($e =3;$e<@array;$e++)
	{
		$tem1 = $array[$e];
	#	$line = $line." ".$array[$e];
		$e++;
		$tem2 = $array[$e];
		$tem3 = 2*$tem1+$tem2;
		$line = $line." ".$tem3;
	#	$line = $line." ".$array[$e];
		$e++;
	}
	print WRBUFF $line."\n";
	$numm_count++;		
	
}
print "check 1 $numm_count\n";
open MBUFF,"< $malefile" or die "no file $malefile";
$line = <MBUFF>;
@marray = split(/\s/,$line);
shift(@marray);
shift(@marray);
$line = <MBUFF>;
$mnum =0;
for($i=0;$i<@marray;$i++)
{
	$male[$mnum] = $marray[$i];
	$mnum++;
	$hashgender{$marray[$i]} = "m"; 
}
open FBUFF,"< $femalefile" or die "no file $femalefile";
$fline = <FBUFF>;
@farray = split(/\s/,$fline);
shift(@farray);
shift(@farray);
shift(@farray);
$fnum = 0;
for($i=0;$i<@farray;$i++)
{
	$fearray[$fnum] = $farray[$i];
	$fnum++;
	
        $hashgender{$farray[$i]} = "f";
	$i++;
	$i++;
}
if($num != $fnum+$mnum)
{
	die "total :$num male : $mnum female : $fnum\n";
}
while($fline = <FBUFF>)
{
	chomp($fline);
	chomp($mline);
	$mline = <MBUFF>;
	@m1array = split(/\s/,$mline);
	shift(@m1array);
	$rsid_male = shift(@m1array);
	for($i=0;$i<@m1array;$i++)
	{
		
		$hashmale{$marray[$i]} = $m1array[$i];
	}
	@f1array = split(/\s/,$fline);
	$rsid_female = shift(@f1array);
	$snp1 = shift(@f1array);
	$snp2 = shift(@f1array);
	if($rsid_male ne $rsid_female)
	{
		die "rsids female and male are not equal\n";
	}	
	$line = "$rsid_female $snp1 $snp2";
	$hash_main{$rsid_female} = 10;
	$k = 0;
	for($i=0;$i<@f1array;$i++)
	{
		$hashfemale{$fearray[$k]} = $f1array[$i]." ".$f1array[$i+1];	
		$k++;
		$i++;
		$i++;	
	}	
	for($i=0;$i<@array_sam;$i++)
	{
		if($hashgender{$array_sam[$i]} eq "m")
		{
			#print $snp1."\t".$hashmale{$array_sam[$i]}."\n";
			if($snp1 eq $hashmale{$array_sam[$i]})
			{
				#$line =$line." 1 0";
				 $line = $line." 2";
			}
			else
			{
				#$line =$line." 0 0";
				 $line = $line." 0";
			}
		}
		else
		{
			@arr = split(/\s/,$hashfemale{$array_sam[$i]});
			$tem = 2*$arr[0]+$arr[1];
			#$line = $line." $arr[0] $arr[1]";
			 $line = $line." $tem";
		}
	}
	print WRBUFF $line."\n";
	$numm_count++;
	#print $line."\n";
}

print "check 2 $numm_count\n";

while($tline = <TBUFF>)
{
	chomp($tline);
	@tp_array = split(/\s/,$tline);
	if($hash_main{$tp_array[1]} eq "10")
	{
	}
	else
	{
		@array_tped_snps = ();
		$num_tped =0;
		if($tline =~ m/A/)
		{
			$array_tped_snps[$num_tped] = "A";
			$num_tped++;
		}
		if($tline =~ m/C/)
                {
                        $array_tped_snps[$num_tped] = "C";
			$num_tped++;
                }
		if($tline =~ m/G/)
                {
                        $array_tped_snps[$num_tped] = "G";
			$num_tped++;
                }
		if($tline =~ m/T/)
                {
                        $array_tped_snps[$num_tped] = "T";
			$num_tped++;
                }
		if($num_tped == 1)
		{
			$tped_snp1 = $array_tped_snps[0];
			if($tped_snp1 eq "A")
			{
				$tped_snp2 = "-";
				
			}
			 if($tped_snp1 eq "T")
                        {
                                $tped_snp2 = "-";
                        }
			 if($tped_snp1 eq "G")
                        {
                                $tped_snp2 = "-";
                        }
			 if($tped_snp1 eq "C")
                        {
                                $tped_snp2 = "-";
                        }

		}
		elsif($num_tped ==0){
                                $tped_snp1 = "-";
                                $tped_snp2 = "-";
                }
		else
		{
			 $tped_snp1 = $array_tped_snps[0];
			 $tped_snp2 = $array_tped_snps[1];
		}
		#$line = "$tp_array[1] $tped_snp1 $tped_snp2";
		$mainar[0] = $tp_array[1];
                $mainar[1] = $tped_snp1;
                $mainar[2] = $tped_snp2;
                $A = 3;
                $A1 = 0;
                $A2 =0;
		for($g=4;$g<@tp_array;$g++)
		{
			$ssnp1 = $tp_array[$g];
			$g++;
			$ssnp2 = $tp_array[$g];	
			if($ssnp1 eq $tped_snp1 && $ssnp2  eq $tped_snp1) 
			{
				#$line =$line." 1 0";
				#$line =$line." 2";
				 $mainar[$A] = "2";
                                 $A++;
                                 $A1++;
			}
			elsif($ssnp1 eq $tped_snp2 && $ssnp2  eq $tped_snp2)
                        {
                                #$line =$line." 0 0";
				#$line =$line." 0";
				$mainar[$A] = "0";
                                $A++;
                                $A2++;
                        }
			elsif($ssnp1 eq "0" && $ssnp2  eq "0")
                        {
                                #$line =$line."  ";
                                $mainar[$A] = "-";
                                $A++;
                        }
			else
			{	#$line =$line." 0 1";
				#$line =$line." 1";
				$mainar[$A] = "1";
                                $A++;
			}
			#print WRBUFF $line."\n";
		}	
			#print WRBUFF $line."\n";
		if($A1 < $A2)
                {
                        $mainar[1] = $tped_snp2;
                        $mainar[2] = $tped_snp1;
			$snpp1 = shift(@mainar);
			$snpp2 = shift(@mainar);
			$snpp3 = shift(@mainar);
                        $line = join(" ",@mainar);
                        $line =~ s/2/3/g;
                        $line =~ s/0/2/g;
                        $line =~ s/3/0/g;
			$line = $snpp1." ".$snpp2." ".$snpp3." ".$line;
                }
                else
                {
                        $line = join(" ",@mainar);
                }

                print WRBUFF $line."\n";
		$numm_count++;
	}
}
print "check 3 $numm_count\n";
